//
//  ViewController.m
//  CoreData36
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager36.h"
#import "Student.h"

@interface ViewController ()

- (IBAction)Add:(UIButton *)sender;
- (IBAction)Select:(UIButton *)sender;
- (IBAction)Update:(UIButton *)sender;
- (IBAction)Delete:(UIButton *)sender;




@property(nonatomic, strong) CoreDataManager36 *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.manager = [CoreDataManager36 shareCoreData];
    
    NSLog(@"manager = %@", [self.manager applicationDocumentsDirectory]);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark    CoreData 的添加方法
- (IBAction)Add:(UIButton *)sender {
    
    //  创建实体描述
    //  参数  1. 描述的实体
    //  参数  2. 数据管理器
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    //  创建实体
    //  参数  1. 实体描述
    //  参数  2. 数据管理器
    
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    stu.name = @"洛牧";
    stu.age = @22;
    stu.sex = @"男";
    stu.number = @1;
    stu.hobby = @"爱好";
    
    [self.manager saveContext];
    
}


#pragma mark    CoreData 的查询方法
- (IBAction)Select:(UIButton *)sender {
    
    
    //  获取查询数据的请求, 相当于数据库中的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    
    
    //  添加一个约束, 谓词(一句话只有对或错, 只有两种答案)
    //  生成约束
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@" ], @"洛牧"];
    //  添加约束
    request.predicate = predicate;
    
    
    
    //  通过某个key给数组排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSLog(@"%ld", array.count);
    
}

#pragma mark    CoreData 的修改方法
- (IBAction)Update:(UIButton *)sender {
    
    
    //  获取查询数据的请求, 相当于数据库中的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    
    
    //  添加一个约束, 谓词(一句话只有对或错, 只有两种答案)
    //  生成约束
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@" ], @"洛牧"];
    //  添加约束
    request.predicate = predicate;
    
    
    
    //  通过某个key给数组排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    

    
    
    //  修改
    Student *stu = [array lastObject];
    stu.sex = @"女";
    [self.manager saveContext];
    
}

#pragma mark    CoreData 的删除方法
- (IBAction)Delete:(UIButton *)sender {
    
    
#if 0
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"洛牧"];
    
    request.predicate = predicate;
    
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
#endif
    
    
    
//  快捷方式 (fet + 回车)
#if 1
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"洛牧"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
#endif
    
    for (Student *stu in array) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];
    
}
@end
